package crp.services.api.graphql.loaders;

import crp.services.api.graphql.models.AccessTokenPayload;
import crp.services.api.graphql.models.UserPayload;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Source;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import java.util.List;

@GraphQLApi
@RequestScoped
public class AccessTokenPayloadLoader {
    private final UserFetcher userFetcher;

    @Inject
    public AccessTokenPayloadLoader(UserFetcher userFetcher) {
        this.userFetcher = userFetcher;
    }

    public List<UserPayload> user(@Source List<AccessTokenPayload> accessTokens) {
        return userFetcher.fetch(accessTokens, accessToken -> accessToken.isUser() ? accessToken.getId() : null);
    }
}
